package com.zxy.supplier_system.server.service

import com.zxy.supplier_system.server.controller.MarketInternalGoodsCodeController
import com.zxy.supplier_system.server.entity.MarketInternalGoodsCode
import com.zxy.supplier_system.server.repository.GoodsRepository
import com.zxy.supplier_system.server.repository.MarketInternalGoodsCodeRepository
import com.zxy.supplier_system.server.repository.MarketRepository
import com.zxy.supplier_system.server.utils.orElse404
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class MarketInternalGoodsCodeService(
    private val marketInternalGoodsCodeRepository: MarketInternalGoodsCodeRepository,
    private val systemServiceService: SystemServiceService,
    private val goodsRepository: GoodsRepository,
    private val marketRepository: MarketRepository
) {

    fun findAllByMarketId(marketId: Int): List<MarketInternalGoodsCodeController.MarketInternalGoodsCodeResponse> {
        val currentSystemService = systemServiceService.getCurrentSystemService()
        return marketInternalGoodsCodeRepository
            .findByMarketIdAndMarket_SystemServiceOrderByCreatedDateTimeDesc(marketId, currentSystemService)
            .map { it.toResponse() }
    }

    fun findByIdAndMarketId(id: Int, marketId: Int): MarketInternalGoodsCodeController.MarketInternalGoodsCodeResponse {
        val currentSystemService = systemServiceService.getCurrentSystemService()
        return marketInternalGoodsCodeRepository
            .findByIdAndMarketIdAndMarket_SystemService(id, marketId, currentSystemService)
            .orElse404("Internal goods code not found")
            .toResponse()
    }

    @Transactional
    fun addInternalGoodsCode(
        marketId: Int,
        request: MarketInternalGoodsCodeController.AddMarketInternalGoodsCodeRequest
    ): MarketInternalGoodsCodeController.MarketInternalGoodsCodeResponse {
        val currentSystemService = systemServiceService.getCurrentSystemService()
        
        // 验证卖场存在且属于当前系统服务
        val market = marketRepository
            .findByIdAndSystemService(marketId, currentSystemService)
            .orElse404()
        
        // 验证商品存在
        val goods = goodsRepository
            .findByIdOrNull(request.goodsId)
            .orElse404()
        
        val entity = MarketInternalGoodsCode().apply {
            this.code = request.code
            this.goods = goods
            this.market = market
        }
        
        return marketInternalGoodsCodeRepository.save(entity).toResponse()
    }

    @Transactional
    fun updateInternalGoodsCode(
        id: Int,
        marketId: Int,
        request: MarketInternalGoodsCodeController.UpdateMarketInternalGoodsCodeRequest
    ): MarketInternalGoodsCodeController.MarketInternalGoodsCodeResponse {
        val currentSystemService = systemServiceService.getCurrentSystemService()
        
        val entity = marketInternalGoodsCodeRepository
            .findByIdAndMarketIdAndMarket_SystemService(id, marketId, currentSystemService)
            .orElse404()
        
        entity.code = request.code
        
        return marketInternalGoodsCodeRepository.save(entity).toResponse()
    }

    @Transactional
    fun deleteInternalGoodsCode(id: Int, marketId: Int) {
        val currentSystemService = systemServiceService.getCurrentSystemService()
        
        val entity = marketInternalGoodsCodeRepository
            .findByIdAndMarketIdAndMarket_SystemService(id, marketId, currentSystemService)
            .orElse404()
        
        marketInternalGoodsCodeRepository.delete(entity)
    }

    private fun MarketInternalGoodsCode.toResponse() = 
        MarketInternalGoodsCodeController.MarketInternalGoodsCodeResponse(
            id = id!!,
            code = code,
            goodsId = goods.id,
            name = goods.name,
            barcode = goods.barcode,
        )
}